package com.biz.primus.ms.payment.channel.allinpay.req;

import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.ms.payment.channel.allinpay.res.AllInPayCardPayPwdRespVo;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.TreeMap;
import java.util.TreeSet;

/**
 * AllInPayCardPay
 *
 * @author JKLiues
 * @date 2017年11月11日
 */
@Getter
@Setter
@Slf4j
public class AllInPayCardPay extends AllInPayRequestBase {

    private static final String API_URL = "http://116.228.64.55:8080/aop/rest";
    /**
     * ;是	AOP分配给应用的AppKey
     */
    private String app_key;
    /**
     * ;是	API接口名称
     */
    private String method = "allinpay.card.paywithpassword.add";
    /**
     * ;是	时间戳，格式为yyyyMMddHHmmss，例如：20110125010101。API服务端允许客户端请求时间误差为30分钟。
     */
    private String timestamp;
    /**
     * ;是	API协议版本，可选值:1.0。
     */
    private String v = "1.0";

    /**
     * ;是	签名版本号 1 ,2,3,4,5每次更新后+1递增
     */
    private String sign_v = "1";
    /**
     * ;是	指定响应格式。默认xml,目前支持格式为xml,json
     */
    private String format = "json";

    /**
     * 交易唯一流水号，用于控制交易的唯一性
     */
    private String order_id;
    /**
     * 交易类型
     * 01-网上支付
     */
    private String type = "01";
    /**
     * 渠道号
     */
    private String chan_no = "4000000002";
    /**
     * 商户号
     */
    private String mer_id;
    /**
     * 商户上送交易时间
     */
    private String mer_tm;
    /**
     * 商户订单号
     */
    private String mer_order_id;
    /**
     * 支付币种
     */
    private String pay_cur = "rmb";
    /**
     * 支付活动
     */
    private String payment_id = "0000000001";
    /**
     * 交易金额
     */
    private String amount;
    /**
     * 卡号(DES加密)
     */
    private String card_id;
    /**
     * 密码(DES加密)
     */
    private String password;
    /**
     * 商户上送的自定义信息
     */
    private String misc;
    /**
     * 商品订单号1
     */
    private String extra_order_id;
    /**
     * 折扣前金额
     */
    private String ori_amount;
    /**
     * 折扣率
     */
    private String discount_rate;
    /**
     * 用户手续费
     */
    private String user_fee;
    /**
     * 商户上送的用户名称
     */
    private String user_name;
    /**
     * 商品名称
     */
    private String goods_name;
    /**
     * 商品种类
     */
    private String goods_type;
    /**
     * 商品号
     */
    private String goods_id;
    /**
     * 商品备注
     */
    private String goods_misc;

    public AllInPayCardPayPwdRespVo execute(String sign) {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("app_key", this.app_key);
        map.put("method", this.method);
        map.put("timestamp", this.timestamp);
        map.put("v", this.v);
        map.put("sign_v", this.sign_v);
        map.put("format", this.format);
        map.put("order_id", this.order_id);
        map.put("type", this.type);
        map.put("mer_id", this.mer_id);
        map.put("mer_tm", this.mer_tm);
        map.put("mer_order_id", this.mer_order_id);
        map.put("pay_cur", this.pay_cur);
        map.put("payment_id", this.payment_id);
        map.put("amount", this.amount);
        map.put("card_id", this.card_id);
        map.put("password", this.password);
        map.put("misc", this.misc);
        map.put("extra_order_id", this.extra_order_id);
        map.put("ori_amount", this.ori_amount);
        map.put("discount_rate", this.discount_rate);
        map.put("user_fee", this.user_fee);
        map.put("user_name", this.user_name);
        map.put("goods_name", this.goods_name);
        map.put("goods_type", this.goods_type);
        map.put("goods_id", this.goods_id);
        map.put("goods_misc", this.goods_misc);
        map.put("sign", sign);
        RestTemplate restTemplate = getRestTemplate();
        String finalUrl = buildUrl(getApiUrl(), map);
        String forObject = restTemplate.getForObject(finalUrl, String.class);
        return JsonUtils.json2Obj(forObject, AllInPayCardPayPwdRespVo.class);
    }

    @Override
    protected String getApiUrl() {
        return API_URL;
    }

    @Override
    public TreeSet<String> getSignParams() {
        return Sets.newTreeSet(Lists.newArrayList(
                "app_key",
                "method",
                "timestamp",
                "v",
                "sign_v",
                "format",
                "order_id",
                "type",
                "mer_id",
                "mer_tm",
                "mer_order_id",
                "pay_cur",
                "payment_id",
                "amount",
                "card_id",
                "password",
                "misc",
                "extra_order_id",
                "ori_amount",
                "discount_rate",
                "user_fee",
                "user_name",
                "goods_name",
                "goods_type",
                "goods_id",
                "goods_misc"
        ));
    }
}